#!/usr/bin/env tclsh

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_execsql_test date-current-date {
  SELECT length(date('now')) = 10;
} {1}

do_execsql_test date-specific-date {
  SELECT date('2023-05-18');
} {2023-05-18}

do_execsql_test date-with-time {
  SELECT date('2023-05-18 15:30:45');
} {2023-05-18}

do_execsql_test date-iso8601 {
  SELECT date('2023-05-18T15:30:45');
} {2023-05-18}

do_execsql_test date-with-milliseconds {
  SELECT date('2023-05-18 15:30:45.123');
} {2023-05-18}

do_execsql_test date-julian-day-integer {
  SELECT date(2460082);
} {2023-05-17}

do_execsql_test date-julian-day-float {
  SELECT date(2460082.5);
} {2023-05-18}

do_execsql_test date-invalid-input {
  SELECT date('not a date');
} {{}}

do_execsql_test date-null-input {
  SELECT date(NULL);
} {{}}

do_execsql_test date-out-of-range {
  SELECT date('10001-01-01');
} {{}}

do_execsql_test date-time-only {
  SELECT date('15:30:45');
} {2000-01-01}

do_execsql_test date-with-timezone-utc {
  SELECT date('2023-05-18 15:30:45Z');
} {2023-05-18}

do_execsql_test date-with-timezone-positive {
  SELECT date('2023-05-18 23:30:45+02:00');
} {2023-05-18}

do_execsql_test date-with-timezone-negative {
  SELECT date('2023-05-19 01:30:45-05:00');
} {2023-05-19}

do_execsql_test date-with-timezone-day-change-positive {
  SELECT date('2023-05-18 23:30:45-03:00');
} {2023-05-19}

do_execsql_test date-with-timezone-day-change-negative {
  SELECT date('2023-05-19 01:30:45+03:00');
} {2023-05-18}

do_execsql_test date-with-timezone-iso8601 {
  SELECT date('2023-05-18T15:30:45+02:00');
} {2023-05-18}

do_execsql_test date-with-timezone-and-milliseconds {
  SELECT date('2023-05-18 15:30:45.123+02:00');
} {2023-05-18}

do_execsql_test date-with-invalid-timezone {
  SELECT date('2023-05-18 15:30:45+25:00');
} {{}}

do_execsql_test date-with-modifier-add-days {
  SELECT date('2023-05-18', '+10 days');
} {2023-05-28}

do_execsql_test date-with-modifier-subtract-days {
  SELECT date('2023-05-18', '-10 days');
} {2023-05-08}

do_execsql_test date-with-multiple-modifiers {
  SELECT date('2023-05-18', '+1 days', '-1 days', '+10 days');
} {2023-05-28}

do_execsql_test date-with-invalid-modifier {
  SELECT date('2023-05-18', 'invalid modifier');
} {{}}

do_execsql_test time-no-arg {
  SELECT length(time()) = 8;
} {1}

do_execsql_test time-current-time {
  SELECT length(time('now')) = 8;
} {1}

do_execsql_test time-specific-time {
  SELECT time('04:02:00');
} {04:02:00}

do_execsql_test time-of-datetime {
  SELECT time('2023-05-18 15:30:45');
} {15:30:45}

do_execsql_test time-iso8601 {
  SELECT time('2023-05-18T15:30:45');
} {15:30:45}

do_execsql_test time-with-milliseconds {
  SELECT time('2023-05-18 15:30:45.123');
} {15:30:45}

do_execsql_test time-julian-day-integer {
  SELECT time(2460082);
} {12:00:00}

do_execsql_test time-julian-day-float {
  SELECT time(2460082.2);
} {16:48:00}

do_execsql_test time-invalid-input {
  SELECT time('not a time');
} {{}}

do_execsql_test time-null-input {
  SELECT time(NULL);
} {{}}

do_execsql_test time-out-of-range {
  SELECT time('25:05:01');
} {{}}

do_execsql_test time-date-only {
  SELECT time('2024-02-02');
} {00:00:00}

do_execsql_test time-with-timezone-utc {
  SELECT time('2023-05-18 15:30:45Z');
} {15:30:45}

do_execsql_test time-with-timezone-positive {
  SELECT time('2023-05-18 23:30:45+07:00');
} {16:30:45}

do_execsql_test time-with-timezone-negative {
  SELECT time('2023-05-19 01:30:45-05:00');
} {06:30:45}

do_execsql_test time-with-timezone-day-change-positive {
  SELECT time('2023-05-18 23:30:45-03:00');
} {02:30:45}

do_execsql_test time-with-timezone-day-change-negative {
  SELECT time('2023-05-19 01:30:45+03:00');
} {22:30:45}

do_execsql_test time-with-timezone-iso8601 {
  SELECT time('2023-05-18T15:30:45+02:00');
} {13:30:45}

do_execsql_test time-with-timezone-and-milliseconds {
  SELECT time('2023-05-18 15:30:45.123+02:00');
} {13:30:45}

do_execsql_test time-with-invalid-timezone {
  SELECT time('2023-05-18 15:30:45+25:00');
} {{}}

do_execsql_test time-with-modifier-start-of-day {
  SELECT time('2023-05-18 15:30:45', 'start of day');
} {00:00:00}

do_execsql_test time-with-modifier-add-hours {
  SELECT time('2023-05-18 15:30:45', '+2 hours');
} {17:30:45}

do_execsql_test time-with-modifier-add-minutes {
  SELECT time('2023-05-18 15:30:45', '+45 minutes');
} {16:15:45}

do_execsql_test time-with-modifier-add-seconds {
  SELECT time('2023-05-18 15:30:45', '+30 seconds');
} {15:31:15}

do_execsql_test time-with-modifier-subtract-hours {
  SELECT time('2023-05-18 15:30:45', '-3 hours');
} {12:30:45}

do_execsql_test time-with-modifier-subtract-minutes {
  SELECT time('2023-05-18 15:30:45', '-15 minutes');
} {15:15:45}

do_execsql_test time-with-modifier-subtract-seconds {
  SELECT time('2023-05-18 15:30:45', '-45 seconds');
} {15:30:00}

do_execsql_test time-with-multiple-modifiers {
  SELECT time('2023-05-18 15:30:45', '+1 hours', '-30 minutes', '+15 seconds');
} {16:01:00}

do_execsql_test time-with-invalid-modifier {
  SELECT time('2023-05-18 15:30:45', 'invalid modifier');
} {{}}

do_execsql_test time-with-invalid-modifier {
  SELECT time('2023-05-18 15:30:45', '+1 hour', 'invalid modifier');
} {{}}

do_execsql_test unixepoch-at-start {
  SELECT unixepoch('1970-01-01');
} {0}

do_execsql_test unixepoch-at-1-second-before-epochtime {
  SELECT unixepoch('1969-12-31 23:59:59');
} {-1}

do_execsql_test unixepoch-at-future {
  SELECT unixepoch('9999-12-31 23:59:59');
} {253402300799}

do_execsql_test unixepoch-at-start-of-time {
  SELECT unixepoch('0000-01-01 00:00:00');
} {-62167219200}

do_execsql_test unixepoch-at-millisecond-precision-input-produces-seconds-precision-output {
  SELECT unixepoch('2022-01-27 12:59:28.052');
} {1643288368}